from urllib.error import URLError
from urllib.request import ProxyHandler, build_opener, HTTPHandler, install_opener ,Request ,urlopen

import time

from catch.read_file import read

def send_picture(proxy_add):
    try:
        num='16'
        fileId='4133088319389773'
        times=int(time.time())
        url='http://upload.api.weibo.com/2/mss/meta_query.json?fid='+fileId+'&source=209678993&callback=IM_'+str(times)\
            +num+'&__rnd='+str(times)

        print('发送URL地址为:%s'%(url))

        #构建上传参数

        cookie_file = open('../file/pic_cookies.txt', 'r')
        cook = cookie_file.readline()

        print('cookie:',cook)

        head={'Accept':'*/*','Accept-Encoding':'gzip, deflate',
              'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
              'Connection':'keep-alive','Host':'upload.api.weibo.com',
              'Referer':'http://weibo.com/u/1800847837/home?wvr=5&lf=reg',
              'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0',
              'Cookie':cook}

        proxy = ProxyHandler({'http': proxy_add})
        opener = build_opener(proxy, HTTPHandler(debuglevel=1))
        install_opener(opener)

        request=Request(headers=head,url=url)
        respon=urlopen(request,timeout=10).read().decode('utf-8')
        print(respon)


    except URLError as e:
        if hasattr(e,'code'):
            print(e.code)
        if hasattr(e,'reason'):
            print(e.reason)

if __name__=='__main__':
    ip = read()
    print(ip)
    send_picture(ip)
